package com.dhcc.sds.base.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.entity.SdsUserDeptCfgEntity;
import com.dhcc.sds.base.service.ISdsUserDeptCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 用户与单病种科室关联关系表
 * 
 * @ClassName: UserDeptCfgController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-06-25 09:45:46
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/userdeptcfg")
public class SdsUserDeptCfgController extends BaseController {

	private String PREFIX = "/backend/sds/userdeptcfg/";

	@Autowired
	private ISdsUserDeptCfgService sdsUserDeptCfgService;
	

    /**
     * 跳转到用户与单病种科室关联关系表列表首页
     */
    @RequestMapping("")
    @Permission("userdeptcfg:list")
    public String index() {
        return PREFIX + "userdeptcfg.html";
    }
	
    /**
     * 跳转到新增用户与单病种科室关联关系表
     */
    @RequestMapping("/to_add")
    @Permission("userdeptcfg:add")
    public String toAdd() {
        return PREFIX + "userdeptcfg_add.html";
    }
	
	/**
     * 跳转到修改用户与单病种科室关联关系表
     */
    @RequestMapping("/to_update/{userDeptCfgId}")
    @Permission("userdeptcfg:update")
    public String toUpdate(@PathVariable Long userDeptCfgId, Model model) {
        SdsUserDeptCfgEntity userDeptCfg = this.sdsUserDeptCfgService.selectById(userDeptCfgId);
        model.addAttribute("userDeptCfg",userDeptCfg);
        return PREFIX + "userdeptcfg_edit.html";
    }

    /**
     * 获取用户与单病种科室关联关系表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("userdeptcfg:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsUserDeptCfgEntity> page = new PageFactory<SdsUserDeptCfgEntity>().defaultPage();
        List<SdsUserDeptCfgEntity> result = this.sdsUserDeptCfgService.page(page,params);
        page.setRecords((List<SdsUserDeptCfgEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增用户与单病种科室关联关系表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("userdeptcfg:add")
    @BussinessLog(value = "新增用户与单病种科室关联关系表")
    public Object add(SdsUserDeptCfgEntity userDeptCfg) {
        if (CommonUtil.isOneEmpty(userDeptCfg)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        userDeptCfg.insert();
        return R.ok();
    }

    /**
     * 删除用户与单病种科室关联关系表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("userdeptcfg:delete")
    @BussinessLog(value = "删除用户与单病种科室关联关系表")
    public Object delete(@RequestParam Long userDeptCfgId) {
	
        this.sdsUserDeptCfgService.deleteById(userDeptCfgId);

        return R.ok();
    }

    /**
     * 修改用户与单病种科室关联关系表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("userdeptcfg:update")
    @BussinessLog(value = "修改用户与单病种科室关联关系表")
    public Object update(SdsUserDeptCfgEntity userDeptCfg) {
        if (CommonUtil.isOneEmpty(userDeptCfg)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsUserDeptCfgEntity old = this.sdsUserDeptCfgService.selectById(userDeptCfg.getId());

        old.setUserId(userDeptCfg.getUserId());
        old.setDepartId(userDeptCfg.getDepartId());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 获取科室的tree列表
     */
    @RequestMapping(value = "/deptTree")
    @ResponseBody
    public List<ZTreeNode> deptTree() {
        return sdsUserDeptCfgService.getDeptTree();
    }
	
}
